Visual Basic 2010 Program: Maze - Create your Maze Without Using Labels
Introduction
In the Visual Studio Help Viewer 1.1 there is an example program called Maze
In order to study this example in tab "Contents" make the following selections:
- Helper Viewer Home, Visual Studio 2010, Visual Studio, Visual Studio Languages, Visual Basic and Visual C#, Getting Started Tutorials, Tutorial 2: Create a Maze., Step 2: Create Your Maze using Labels.
In Step 2 you build the maze by using Label controls for each wall. In this example the maze has 40 walls, that means 40 Labels. Those Labels are called: Label1 until Label40. The building of this maze is a very time consuming effort.
What is worse: Label controls do not support the array concept.
- Suppose you want to change the colors of the walls. There is no easy way to do that only a very time consuming way.
- Suppose you want to change the layout of your maze. There again is no easy way to do that.
The program Maze discussed here solves both problems. The whole idea is to use the Toolbox as less as possible.
In fact the whole program is based on features that were also available in Visual Basic 5.0.
The only new feature from Visual Basic 2010 that it uses is the BitMap. The Bitmap is used to store and save the maze.
VB Program: Maze - Data Base Description
The program contains two forms: Maze Form1 and Maze Form2.
- Maze Form1 is used to control the Maze. This Form uses the following tools:
- 2 buttons: Start and End.
The Start button is used to start one play and to build the maze.
The End button is used to end the program
- 4 TextBoxes : TBSize, TBFree, TBTimer and TBError
- TBSize contains the size of the maze. The size has to be even. Maximum value is 40. Minimum value = 6
- TBFree contains the number of free cells during maze building.
- TBTimer contains the time in seconds for to solve the maze
- TBError contains the number of errors to solve the maze. There are two types of errors:
- Errors caused when you hit a wall
- Errors caused when you go in the wrong direction.
- 4 Labels: LSize, LFree, LTimer and LError
- 1 PictureBox. This picture box shows 4 arrows. The direction of the arrows is:
- At 3 o'clock, At 6 o'clock, At 9 o'clock and at 12 o'clock
- Maze Form2 is used to display the Maze. This Form uses the following tools:
- 1 PictureBox. This picture box shows the maze.
Besides tools the program uses variables. The most important variables are:
- maze(ixx,iyy) This two dimensional array is the maze. The following values are used for each cell:
- -1: not to be used for a wall.
- 0 : not being used.
- 1 : this is a wall
- 2 : dead end street.
- 3 : solution path
- 4 : value used for the water test.
- 5 : cursor (black)
- bmpl container bitmap(1000,1000). This bitmap of form2 contains the maze
- bmps container bitmap(100,100). This bitmap of form1 contains the 4 arrows.
The following pictures show the initial set up and the final setup
1 1 1 1 1 1 1
- - - - 1
1 1 1 1
1 - - - 1
1 1 1 1
1 - - - -
1 1 1 1 1 1 1
Figure 1
|
|
1 1 1 1 1 1 1
- - - - 2 2 1
1 2 1 - 1 2 1
1 2 1 - 1 2 1
1 1 1 - 1 1 1
1 2 2 - - - -
1 1 1 1 1 1 1
Figure 2
|
|
- Figure 1 shows the initial set up.
There are 4 walls identified with the value 1.
There are also 4 pillars. Also identified with the value 1
There are 11 cells with the value -1. Those free cells should not be used to place a wall.
There are 12 cells with the value 0. Those cells are free.
- Figure 2 shows the final situation.
There are 4 cells which changed from 0 to 1. Those cells are now walls.
There are 8 cells which now contain the value 2. Those cells show dead end street. When you enter those cells you have to return.
There are 11 cells with the value -1. Those cells should not be used to place a wall. In this situation they show the solution.
There are no free cells. This is important. That means figure 2 shows the final maze.
VB Program Maze: Program Operation
Operation of the program consists of two phases: Maze building and Play.
- Maze building starts when the start pushbutton is selected.
Before the start is selected the size value can be changed to select the size of the maze.
During Maze building the Free value changes from a high value to zero. When zero maze building is completed and the new maze is drawn. Next Play starts
- Phase Play. starts when the maze is drawn. The top left is the start position with the black cursor. The bottom right corner is the finish. Also shown are 4 arrows in the different directions.
- One arrow in the right direction. This is in the 3 o'clock direction. Delta x,y = 1,0
- One arrow in the left direction. This is in the 9 o'clock direction. Delta x,y = -1,0
- One arrow in the up direction. This is in the 12 o'clock direction. Delta x,y = 0,-1
- One arrow in the down direction. This is in the 6 o'clock direction. Delta x,y = 0,1
When an arrow is selected the cursor moves in the selected direction.
The play is over when the finish (bottom right corner) is reached.
Also show on the display are a Timer and an Error value.
- The Timer value is updated each time when an arrow is selected.
- The Error value is updated each time when a wall is hit.
At the end of the play the Error value is updated with how many times a cell outside the solution path is selected. If you consider figure 2 this are the cells with the value 2.
VB Program Maze: Program description
Program Maze contains the following subroutines:
Reflection
The importance of this exercise is to use a small number of tools and a minimum instruction set. The second objective of this exercise is demonstrate the importance of mathematics and to subdivide the problem in subroutines. In fact the mathematics involved to calculate the maze which only has one solution to go from start to finish is what it is all about.
When you study Visual Basic 2010 there is a huge toolbox which in fact has nothing to do with the application.
The reason of this program was a document written by David Platt in the MSDN magazine. For a review of that document go to The silent Majority
For a review about Visual Basic 2010 in General read this: Visual Basic 2010 Evaluation and Criticism
Feedback
No feedback received.
First Release: 25 August 2012
Go back to: Visual Basic 2010 Evaluation and Criticism How to draw a point.
Back to my home page Contents of This Document